﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using DataMongo;
using DataMongo.Entities;

namespace BreakingHubAdmin
{
    public class RssReader
    {
        public static IEnumerable<Rss> GetRssFeed(string feedUrl)
        {

            try
            {

                WebClient client = new WebClient();
                string result = client.DownloadString(feedUrl);
                // Convert string to XDocument.
                XDocument xmlDoc = XDocument.Parse(result);
                XNamespace m = "http://search.yahoo.com/mrss/";

                var rssKing = (from story in xmlDoc.Descendants("item")
                    select new Rss
                    {
                        Title = ((string) story.Element("title")),
                        Link = ((string) story.Element("link")),
                        Description = ((string) story.Element("description")),
                        PublishDate = ((string) story.Element("pubDate")),
                        ImageUrl = story.Element(m + "thumbnail").Attribute("url").Value
                        
                    }).Take(20).ToList();

                return rssKing.ToList();
            }
            catch (Exception e)
            {

            }
            return null;
        }
    }
}